// Author: Wilfred Pinto
// Website: http://projectsilverlight.blogspot.com

using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Xml;

namespace SimpleViewer_SL.code
{
    /// <summary>
    /// The object to store the name and caption for each image
    /// </summary>
    public class GalleryImageSettings
    {
        /// <summary>
        /// The 'name' of the image
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// The 'caption' of the image
        /// </summary>
        public string Caption { get; set; }

        /// <summary>
        /// Initializes the name and caption to empty strings
        /// </summary>
        public GalleryImageSettings()
        {
            Name = string.Empty;
            Caption = string.Empty;
        }
    }

    /// <summary>
    /// The object to store the settings information for the entire gallery
    /// </summary>
    public class GallerySettings
    {
        /// <summary>
        /// A completely transparent image
        /// </summary>
        public Color NoColor = Color.FromArgb(0, 0, 0, 0);

        /// <summary>
        /// Width of the widest image in the gallery. 
        /// Used to determine the best layout for your gallery (pixels).
        /// Default Value: 480
        /// </summary>
        public int MaxImageWidth { get; private set; }
        
        /// <summary>
        /// Height of tallest image in the gallery. 
        /// Used to determine the best layout for your gallery (pixels).
        /// Default Value: 480
        /// </summary>
        public int MaxImageHeight { get; private set; }
        
        /// <summary>
        /// Color of title and caption text (hexadecimal color value).
        /// Default Value: 0xffffff (White)
        /// </summary>
        public Color TextColor { get; private set; }
        
        /// <summary>
        /// Color of image frame, navigation buttons and thumbnail frame (hexadecimal color value).
        /// Default Value: 0xffffff (White)
        /// </summary>
        public Color FrameColor { get; private set; }
        
        /// <summary>
        /// Width of image frame (pixels).
        /// Default Value: 20
        /// </summary>
        public int FrameWidth { get; private set; }
        
        /// <summary>
        /// Width of padding around gallery edge (pixels). 
        /// To have the image flush to the edge of the swf, set this to 0.
        /// Default Value: 40
        /// </summary>
        public int StagePadding { get; private set; }
        
        /// <summary>
        /// Distance between image and thumbnails (pixels).
        /// Default Value: 40
        /// </summary>
        public int NavPadding { get; private set; }
        
        /// <summary>
        /// Width of padding around thumbnail (pixels).
        /// </summary>
        public int ThumbPadding { get; private set; }
        
        /// <summary>
        /// Number of thumbnail columns. 
        /// To disable thumbnails completely set this value to 0.
        /// Default Value: 3
        /// </summary>
        public int ThumbnailColumns { get; private set; }
        
        /// <summary>
        /// Number of thumbnail rows. 
        /// To disable thumbnails completely set this value to 0.
        /// Default Value: 3
        /// </summary>
        public int ThumbnailRows { get; private set; }
        
        /// <summary>
        /// Position of thumbnails relative to image. 
        /// Can be "top", "bottom","left" or "right".
        /// </summary>
        public NavPosition NavPosition { get; private set; }
        
        /// <summary>
        /// Vertical placment of the image and thumbnails within the SWF. Can be "center", "top" or "bottom". 
        /// For large format galleries this is best set to "center". 
        /// For small format galleries setting this to "top" or "bottom" can help get the image flush to the edge of the swf.
        /// Default Value: Center
        /// </summary>
        public Align VAlign { get; private set; }
        
        /// <summary>
        /// Horizontal placement of the image and thumbnails within the SWF. Can be "center", "left" or "right".
        /// For large format galleries this is best set to "center". 
        /// For small format galleries setting this to "left" or "right" can help get the image flush to the edge of the swf.
        /// Default Value: Center
        /// </summary>
        public Align HAlign { get; private set; }
        
        /// <summary>
        /// Text to display as gallery Title.
        /// </summary>
        public string Title { get; private set; }
        
        /// <summary>
        /// Whether to display a 'Open In new Window...' dialog when right-clicking on an image. 
        /// Can be "true" or "false"
        /// Default Value: True
        /// </summary>
        public bool EnableRightClickOpen { get; private set; }
        
        /// <summary>
        /// Relative or absolute path to a JPG or SWF to load as the gallery background.
        /// Relative paths are relative to the HTML document that contains SimpleViewer. 
        /// For example: "images/bkgnd.jpg".
        /// Default Value: ""
        /// </summary>
        public string BackgroundImagePath { get; private set; }
        
        /// <summary>
        /// Relative or absolute path to images folder. This value is only required if you don't want to put your images in the 'images' folder.
        /// Relative paths are relative to the HTML document that contains SimpleViewer-SL. 
        /// Note that this path should include a trailing forward slash. 
        /// For example: imagePath="my_image_folder/".
        /// Default Value: "images/"
        /// </summary>
        public string ImagePath { get; private set; }
        
        /// <summary>
        /// Relative or absolute path to thumbnail images folder. This value is only required if you don't want to put your thumbnail images in the 'thumbs' folder.
        /// Relative paths are relative to the HTML document that contains SimpleViewer-SL. 
        /// Note that this path should include a trailing forward slash. 
        /// For example: thumbPath="my_thumb_folder/".
        /// Default Value: "thumbs/"
        /// </summary>
        public string ThumbPath { get; private set; }
        
        /// <summary>
        /// Color of image and thumbnail band (hexadecimal color value).
        /// Default Value: No Color
        /// </summary>
        public Color ImageAndThumbBandBackgroundColor { get; private set; }
        
        /// <summary>
        /// Color of image background (hexadecimal color value).
        /// Default Value: No Color
        /// </summary>
        public Color ImageBackgroundColor { get; private set; }
        
        /// <summary>
        /// Color of thumbnail background (hexadecimal color value).
        /// Default Value: No Color
        /// </summary>
        public Color ThumbBackgroundColor { get; private set; }
        
        /// <summary>
        /// Stores the name and captions of each image
        /// </summary>
        public List<GalleryImageSettings> ImagesSettings { get; private set; }

        /// <summary>
        /// GallerySettings constructor.
        /// Sets the default values for each settings variable.
        /// </summary>
        public GallerySettings()
        {
            MaxImageWidth = 480;
            MaxImageHeight = 480;
            TextColor = Color.FromArgb(255, 255, 255, 255);
            FrameColor = Color.FromArgb(255, 255, 255, 255);
            FrameWidth = 20;
            StagePadding = 40;
            NavPadding = 40;
            ThumbPadding = 8;
            ThumbnailColumns = 3;
            ThumbnailRows = 3;
            NavPosition = NavPosition.Left;
            VAlign = Align.Center;
            HAlign = Align.Center;
            Title = "";
            EnableRightClickOpen = true;
            BackgroundImagePath = string.Empty;
            ImagePath = "images/";
            ThumbPath = "thumbs/";
            ImageAndThumbBandBackgroundColor = ImageBackgroundColor = ThumbBackgroundColor = NoColor;
            ImagesSettings = new List<GalleryImageSettings>();
        }

        /// <summary>
        /// Parses the settings XML file into a Ojbect
        /// </summary>
        /// <param name="reader">The settings xml file encapsulated in a XmlReader</param>
        /// <example>
        ///     An example of a Settings File (gallery.xml)
        ///     <code>
        ///     &lt;?xml version="1.0" encoding="utf-8"?&gt;
        ///     &lt;simpleviewerGallery maxImageWidth="400" maxImageHeight="300" textColor="0x181818" frameColor="0x555555" 
        ///         frameWidth="20" stagePadding="5" thumbnailColumns="3" thumbnailRows="3" 
        ///         title="Ava's photos" enableRightClickOpen="false" backgroundImagePath="" imagePath="" thumbPath="" imageAndThumbBandBackgroundColor="0xD2C2C5"&gt;
        ///         &lt;image&gt;
        ///             &lt;name&gt;1.jpg&lt;/name&gt;
        ///             &lt;caption&gt;Caption for image 1&lt;/caption&gt;
        ///         &lt;/image&gt;
        ///         &lt;image&gt;
        ///             &lt;name&gt;2.jpg&lt;/name&gt;
        ///             &lt;caption&gt;Caption for image 2&lt;/caption&gt;
        ///         &lt;/image&gt;
        ///     &lt;/simpleviewerGallery&gt;
        ///     </code>
        /// </example>
        public void ParseSettingsXmlFile(XmlReader reader)
        {
            // Navigate to the main node
            reader.ReadToFollowing("simpleviewerGallery");

            // Read the attributes
            MaxImageWidth = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("maxImageWidth"), MaxImageWidth);
            MaxImageHeight = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("maxImageHeight"), MaxImageHeight);
            TextColor = SettingsHelper.HexStringToColorWithDefault(reader.GetAttribute("textColor"), TextColor);
            FrameColor = SettingsHelper.HexStringToColorWithDefault(reader.GetAttribute("frameColor"), FrameColor);
            FrameWidth = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("frameWidth"), FrameWidth);
            StagePadding = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("stagePadding"), StagePadding);
            NavPadding = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("navPadding"), NavPadding);
            ThumbPadding = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("thumbPadding"), ThumbPadding);
            ThumbnailColumns = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("thumbnailColumns"), ThumbnailColumns);
            ThumbnailRows = SettingsHelper.GetAttributeIntWithDefault(reader.GetAttribute("thumbnailRows"), ThumbnailRows);
            NavPosition = SettingsHelper.GetNavPositionWithDefault(reader.GetAttribute("navPosition"), NavPosition);
            VAlign = SettingsHelper.GetAlignWithDefault(false, reader.GetAttribute("vAlign"), VAlign);
            HAlign = SettingsHelper.GetAlignWithDefault(true, reader.GetAttribute("hAlign"), HAlign);
            Title = SettingsHelper.GetAttributeStringWithDefault(reader.GetAttribute("title"), Title);
            EnableRightClickOpen = bool.Parse(reader.GetAttribute("enableRightClickOpen"));
            BackgroundImagePath = SettingsHelper.GetAttributeStringWithDefault(reader.GetAttribute("backgroundImagePath"), BackgroundImagePath);
            ImagePath = SettingsHelper.GetAttributeStringWithDefault(reader.GetAttribute("imagePath"), ImagePath);
            ThumbPath = SettingsHelper.GetAttributeStringWithDefault(reader.GetAttribute("thumbPath"), ThumbPath);
            ImageAndThumbBandBackgroundColor = SettingsHelper.HexStringToColorWithDefault(reader.GetAttribute("imageAndThumbBandBackgroundColor"), ImageAndThumbBandBackgroundColor);
            ImageBackgroundColor = SettingsHelper.HexStringToColorWithDefault(reader.GetAttribute("imageBackgroundColor"), ImageBackgroundColor);
            ThumbBackgroundColor = SettingsHelper.HexStringToColorWithDefault(reader.GetAttribute("thumbBackgroundColor"), ThumbBackgroundColor);

            // Parse the image (name and caption) collection
            while (reader.ReadToFollowing("image"))
            {
                // the using keyword disposes off the stream properly
                using (XmlReader imageReader = reader.ReadSubtree())
                {
                    GalleryImageSettings imageSettings = new GalleryImageSettings();

                    imageReader.ReadToFollowing("name");
                    imageSettings.Name = imageReader.ReadElementContentAsString();

                    if (imageReader.ReadToFollowing("caption"))
                        imageSettings.Caption = imageReader.ReadElementContentAsString();

                    ImagesSettings.Add(imageSettings);
                }
            }
        }
    }
}